var starttime = []
var geoDim = []// 地理纬度子集列表
var HB_province = '河北省'
var data_JSON = {}

var innetwork_rate_tdd_sum = ''//
var innetwork_rate_fdd_sum = ''
var innetwork_rate_gsm_sum = ''//
var innetwork_rate_nb_sum = ''//
var innetwork_rate_sum = ''// 综合在网率
var innetwork_rate_tdd = []//
var innetwork_rate_fdd = []
var innetwork_rate_gsm = []//
var innetwork_rate_nb = []//
var innetwork_rate = []// 综合在网率

var serious_warning_rate_1d_sum = ''// 严重告警占比(不含退服)24小时(1天)
var serious_warning_rate_7d_sum = ''
var serious_warning_rate_1m_sum = ''//
var serious_warning_rate_1d = []//
var serious_warning_rate_7d = []
var serious_warning_rate_1m = []//

var available_rate_tdd_sum = ''//
var available_rate_fdd_sum = ''
var available_rate_gsm_sum = ''//
var available_rate_nb_sum = ''//
var available_rate_sum = ''// 综合可用率
var available_rate_tdd = []//
var available_rate_fdd = []
var available_rate_gsm = []//
var available_rate_nb = []//
var available_rate = []// 综合可用率

var withdrawal_rate_gsm_sum = ''// 2G退服
var withdrawal_rate_lte_sum = ''// 4G退服
var withdrawal_rate_gsm = []//
var withdrawal_rate_lte = []

var new_res_benefit_succ_rate = []// 新建资源效益达成率
var expansion_res_benefit_succ_rate = []// 扩容
var newaccess_res_benefit_succ_rate = []// 新入网

N.indexHome = N.Class.extend({
    resize: function () {
        // var topheight = $('.container-fluid').height() - $('.content-header').height() - 5;
        var topheight = $('.container-fluid').height() - 20
        var bottomheight = topheight * (1 / 2)
        var boxtop = bottomheight - 120
        var maptop = topheight * (2 / 3) - 60
        var mapbot = topheight * (1 / 3) - 60
        $('.control-size-box').height(boxtop)
        $('.control-size-box-map').height(maptop)
        $('.control-size-box-mapbot').height(mapbot)

        var headheight = $('#table1_wrapper .dataTables_scrollHeadInner').height()
        if (headheight) {
            this.tablbeHeight = boxtop - headheight - 5
            $('#table1_wrapper .dataTables_scrollBody').height(this.tablbeHeight)
        }
        headheight = $('#table2_wrapper .dataTables_scrollHeadInner').height()
        if (headheight) {
            this.tablbeHeight = boxtop - headheight - 5
            $('#table2_wrapper .dataTables_scrollBody').height(this.tablbeHeight)
        }
    },
    pageLoad: function (geography, shiduan, geo_dim, city, area) {
        loading.show()
        if (geography) {
            this.geographyChart(shiduan, geo_dim, city, area)
        } else {
            this.timeChart(shiduan, geo_dim, city, area)
        }
        this.chartTop(shiduan, geo_dim, city, area)
    },
    chartTop: function (shiduan, geo_dim, city, area) {
        var param
        if (geo_dim === '省') {
            param = {
                geo_dim: geo_dim,
                time_dim: shiduan,
                area: area
            }
        } else {
            param = {
                geo_dim: geo_dim,
                time_dim: shiduan,
                city: city,
                area: area
            }
        }
        service.get(getApiPath() + 'wideTable/geographyChart',
            {
                params: param
            }
        ).then(function (res) {
            res = res.data
            try {
                setBaseVal(res)
            } catch (e) {
                console.log(e)
            }
            loading.hide()
        }).catch(function (e) {
            return false
        })
    },
    geographyChart: function (shiduan, geo_dim, city, area) {
        var param
        if (geo_dim === '省') {
            param = {
                geo_dim: '地市',
                time_dim: shiduan,
                area: area
            }
        } else if (geo_dim === '地市' || geo_dim === '区县') {
            param = {
                geo_dim: '区县',
                time_dim: shiduan,
                city: city,
                area: area != '全量小区' ? area : ''
            }
            /* param = {
                geo_dim: geo_dim,//isDbClickMap ? geo_dim : '区县',
                time_dim: shiduan,
                city: city,
                area: area//area != '全量小区'? area : ''
            } */
        }
        getWideData(param, 'wideTable/geographyChart')
    },
    timeChart: function (shiduan, geo_dim, city, area) {
        var param
        if (geo_dim === '省') {
            param = {
                geo_dim: geo_dim,
                time_dim: shiduan,
                area: area,
                num: shiduan == 'M' ? 6 : 7
            }
        } else {
            param = {
                geo_dim: geo_dim,
                time_dim: shiduan,
                city: city,
                area: area,
                num: shiduan == 'M' ? 6 : 7
            }
        }
        getWideData(param, 'wideTable/timeChart')
    }
})
var page = new N.indexHome()
$(function () {
    loading.show()
    $(document).on('map:click', function (event, geography, shiduan, geo_dim, map_city, map_area) {
        page.pageLoad(geography, shiduan, geo_dim, map_city, map_area)
    })
})

function cleanRes () {
    starttime = []
    geoDim = []// 地理纬度子集列表

    innetwork_rate_tdd_sum = ''//
    innetwork_rate_fdd_sum = ''
    innetwork_rate_gsm_sum = ''//
    innetwork_rate_nb_sum = ''//
    innetwork_rate_sum = ''// 综合在网率
    innetwork_rate_tdd = []//
    innetwork_rate_fdd = []
    innetwork_rate_gsm = []//
    innetwork_rate_nb = []//
    innetwork_rate = []// 综合在网率

    serious_warning_rate_1d_sum = ''// 严重告警占比(不含退服)24小时(1天)
    serious_warning_rate_7d_sum = ''
    serious_warning_rate_1m_sum = ''//
    serious_warning_rate_1d = []//
    serious_warning_rate_7d = []
    serious_warning_rate_1m = []//

    available_rate_tdd_sum = ''//
    available_rate_fdd_sum = ''
    available_rate_gsm_sum = ''//
    available_rate_nb_sum = ''//
    available_rate_sum = ''// 综合在网率
    available_rate_tdd = []//
    available_rate_fdd = []
    available_rate_gsm = []//
    available_rate_nb = []//
    available_rate = []// 综合在网率

    withdrawal_rate_gsm_sum = ''// 2G退服
    withdrawal_rate_lte_sum = ''// 4G退服
    withdrawal_rate_gsm = []//
    withdrawal_rate_lte = []

    new_res_benefit_succ_rate = []// 新建资源效益达成率
    expansion_res_benefit_succ_rate = []// 扩容
    newaccess_res_benefit_succ_rate = []// 新入网
}

function setBaseVal (res) {
    $('#innetwork_rate_tdd_sum').html(res[0].innetwork_rate_tdd)// 基站数
    $('#innetwork_rate_fdd_sum').html(res[0].innetwork_rate_fdd)// 小区数
    $('#innetwork_rate_gsm_sum').html(res[0].innetwork_rate_gsm)// 高干扰小区环比
    $('#innetwork_rate_nb_sum').html(res[0].innetwork_rate_nb)// 高干扰小区数
    $('#innetwork_rate_sum').html(res[0].innetwork_rate)// 劣化小区环比
    $('#serious_warning_rate_1d_sum').html(res[0].serious_warning_rate_1d)// 劣化小区数
    $('#serious_warning_rate_7d_sum').html(res[0].serious_warning_rate_7d)// 4G客户数
    $('#serious_warning_rate_1m_sum').html(res[0].serious_warning_rate_1m)// 4G不限量客户数(万)

    $('#available_rate_tdd_sum').html(res[0].available_rate_tdd)// 基站数
    $('#available_rate_fdd_sum').html(res[0].available_rate_fdd)// 小区数
    $('#available_rate_gsm_sum').html(res[0].available_rate_gsm)// 高干扰小区环比
    $('#available_rate_nb_sum').html(res[0].available_rate_nb)// 高干扰小区数
    $('#available_rate_sum').html(res[0].available_rate)// 劣化小区环比

    $('#withdrawal_rate_gsm_sum').html(res[0].withdrawal_rate_gsm)// 高干扰小区数
    $('#withdrawal_rate_lte_sum').html(res[0].withdrawal_rate_lte)// 劣化小区环比
}

function getWideData (param, reqUrl) {
    console.log('时间维度:' + param.time_dim + '；地理纬度:' + param.geo_dim + '；省:' + HB_province + '；市:' + param.city + '；区县:' + param.area + '；请求地址:' + reqUrl)
    loading.show()
    service.get(getApiPath() + reqUrl,
        {
            params: param
        }
    ).then(function (res) {
        cleanRes()
        res = res.data
        if (res.length >= 1) {
            /* if(isSetBaseVal(reqUrl)){
                setBaseVal(res);
            } */
            for (var i = res.length - 1; i > -1; i--) {
                setVal(res[i])
            }
        }
        drawPage()
    }).catch(function (e) {
        return false
    })
}

function drawPage () {
    // 无线设备在网率
    data_JSON = {
        id: 'line1',
        title: null,
        cate: $('#showFont').attr('geography') ? geoDim : starttime,
        data: [
            {
                name: 'TDD(%)',
                type: 'column',
                maxPointWidth: 10,
                color: N.Util.Theme_colors.blue,
                data: innetwork_rate_tdd,
                yAxis: 0,
                axisLabelY: '%'
            }, {
                name: 'FDD(%)',
                type: 'column',
                maxPointWidth: 10,
                color: N.Util.Theme_colors.red,
                data: innetwork_rate_fdd,
                yAxis: 0,
                axisLabelY: '%'
            }, {
                name: 'GSM(%)',
                type: 'column',
                maxPointWidth: 10,
                color: N.Util.Theme_colors.orange,
                data: innetwork_rate_gsm,
                yAxis: 0,
                axisLabelY: '%'
            }, {
                name: 'NB(%)',
                type: 'column',
                maxPointWidth: 10,
                color: N.Util.Theme_colors.note,
                data: innetwork_rate_nb,
                yAxis: 0,
                axisLabelY: '%'
            },
            {
                name: '综合(%)',
                type: 'line',
                marker: {
                    enabled: true, /* 数据点是否显示 */
                    radius: 4 /* 数据点大小px */
                },
                color: N.Util.Theme_colors.darkGreen,
                data: innetwork_rate,
                yAxis: 0,
                axisLabelY: '%'
            }
        ],
        marginTop: 30,
        marginBottom: 0,
        itemWidth: 110,
        lenabled: true,
        lenabledAlign: 'center',
        lenabledVerticalAlign: '',
        layout: 'horizontal',
        lenabledWidth: 10,
        chartType: 'column',
        Yfloor: 0,
        Yceiling: 120,
        YsoftMax: 120,
        itemDistance: 0

    }
    drawColumnCol(data_JSON)
    // 严重影响业务告警占比（不含退服）
    drawColumnCol({
        id: 'line2',
        title: null,
        cate: $('#showFont').attr('geography') ? geoDim : starttime,
        data: [
            {
                name: '超24小时(%)',
                type: 'line', // areaspline
                color: N.Util.Theme_colors.blue,
                fillOpacity: 0.3, // 指定面积图的透明度
                marker: {
                    enabled: true,
                    symbol: 'circle',
                    radius: 2
                },
                data: serious_warning_rate_1d
            }, {
                name: '超7天(%)',
                type: 'line',
                color: N.Util.Theme_colors.orange,
                fillOpacity: 0.3, // 指定面积图的透明度
                marker: {
                    enabled: true,
                    symbol: 'circle',
                    radius: 2
                },
                data: serious_warning_rate_7d
            }, {
                name: '超1个月(%)',
                type: 'line',
                color: N.Util.Theme_colors.red,
                fillOpacity: 0.3, // 指定面积图的透明度
                marker: {
                    enabled: true,
                    symbol: 'circle',
                    radius: 2
                },
                data: serious_warning_rate_1m
            }
        ],
        marginTop: 30,
        marginBottom: 35,
        itemWidth: 110,
        lenabled: true,
        lenabledAlign: 'center',
        lenabledVerticalAlign: 'top',
        layout: 'horizontal',
        Yfloor: 0,
        Yceiling: 120,
        YsoftMax: 120
    })

    // 无线设备可用率
    data_JSON = {
        id: 'line5',
        title: null,
        cate: $('#showFont').attr('geography') ? geoDim : starttime,
        data: [
            {
                name: 'TDD(%)',
                type: 'column',
                maxPointWidth: 10,
                color: N.Util.Theme_colors.blue,
                data: available_rate_tdd,
                yAxis: 0
            }, {
                name: 'FDD(%)',
                type: 'column',
                maxPointWidth: 10,
                color: N.Util.Theme_colors.red,
                data: available_rate_fdd,
                yAxis: 0
            }, {
                name: 'GSM(%)',
                type: 'column',
                maxPointWidth: 10,
                color: N.Util.Theme_colors.orange,
                data: available_rate_gsm,
                yAxis: 0
            }, {
                name: 'NB(%)',
                type: 'column',
                maxPointWidth: 10,
                color: N.Util.Theme_colors.note,
                data: available_rate_nb,
                yAxis: 0
            },
            {
                name: '综合(%)',
                type: 'line',
                marker: {
                    enabled: true, /* 数据点是否显示 */
                    radius: 4 /* 数据点大小px */
                },
                color: N.Util.Theme_colors.darkGreen,
                data: available_rate,
                yAxis: 0
            }
        ],
        marginTop: 30,
        marginBottom: 0,
        itemWidth: 110,
        lenabled: true,
        lenabledAlign: 'center',
        lenabledVerticalAlign: '',
        layout: 'horizontal',
        lenabledWidth: 10,
        chartType: 'column',
        Yfloor: 0,
        Yceiling: 120,
        YsoftMax: 120,
        itemDistance: 0

    }
    drawColumnCol(data_JSON)
    // 退服率
    drawColumnCol({
        id: 'line6',
        title: null,
        cate: $('#showFont').attr('geography') ? geoDim : starttime,
        data: [
            {
                name: '2G退服率(%)',
                type: 'areaspline',
                color: N.Util.Theme_colors.blue,
                fillOpacity: 0.3, // 指定面积图的透明度
                marker: {
                    enabled: true,
                    symbol: 'circle',
                    radius: 2
                },
                data: withdrawal_rate_gsm
            }, {
                name: '4G退服率(%)',
                type: 'areaspline',
                color: N.Util.Theme_colors.orange,
                fillOpacity: 0.3, // 指定面积图的透明度
                marker: {
                    enabled: true,
                    symbol: 'circle',
                    radius: 2
                },
                data: withdrawal_rate_lte
            }
        ],
        marginTop: 30,
        marginBottom: 35,
        itemWidth: 110,
        lenabled: true,
        lenabledAlign: 'center',
        lenabledVerticalAlign: 'top',
        layout: 'horizontal',
        Yfloor: 0,
        Yceiling: 120,
        YsoftMax: 120
    })
    // 效益评估类指标
    data_JSON = {
        id: 'line4',
        title: null,
        cate: $('#showFont').attr('geography') ? geoDim : starttime,
        data: [
            {
                name: '新建资源效益达成率(%)',
                type: 'line',
                maxPointWidth: 10,
                color: N.Util.Theme_colors.blue,
                data: new_res_benefit_succ_rate,
                yAxis: 0
            }, {
                name: '扩容资源效益达成率(%)',
                type: 'line',
                maxPointWidth: 10,
                color: N.Util.Theme_colors.red,
                data: expansion_res_benefit_succ_rate,
                yAxis: 0
            }, {
                name: '新入网资源效益达成率(%)',
                type: 'line',
                maxPointWidth: 10,
                color: N.Util.Theme_colors.orange,
                data: newaccess_res_benefit_succ_rate,
                yAxis: 0
            }
        ],
        marginTop: 30,
        marginBottom: 0,
        itemWidth: 110,
        lenabled: true,
        lenabledAlign: 'center',
        lenabledVerticalAlign: '',
        layout: 'horizontal',
        lenabledWidth: 10,
        chartType: 'column',
        Yfloor: 0,
        Yceiling: 120,
        YsoftMax: 120

    }
    drawColumnCol(data_JSON)
    loading.hide()
}

function setVal (item) {
    starttime.push(item.starttime.substr(5, 5))
    item.area === '全量小区' ? geoDim.push(item.city) : geoDim.push(item.area)

    innetwork_rate_tdd.push(item.innetwork_rate_tdd)
    innetwork_rate_fdd.push(item.innetwork_rate_fdd)
    innetwork_rate_gsm.push(item.innetwork_rate_gsm)
    innetwork_rate_nb.push(item.innetwork_rate_nb)
    innetwork_rate.push(item.innetwork_rate)


    serious_warning_rate_1d.push(item.serious_warning_rate_1d)
    serious_warning_rate_7d.push(item.serious_warning_rate_7d)
    serious_warning_rate_1m.push(item.serious_warning_rate_1m)

    available_rate_tdd.push(item.available_rate_tdd)
    available_rate_fdd.push(item.available_rate_fdd)
    available_rate_gsm.push(item.available_rate_gsm)
    available_rate_nb.push(item.available_rate_nb)
    available_rate.push(item.available_rate)

    withdrawal_rate_gsm.push(item.withdrawal_rate_gsm)
    withdrawal_rate_lte.push(item.withdrawal_rate_lte)

    new_res_benefit_succ_rate.push(item.new_res_benefit_succ_rate)
    expansion_res_benefit_succ_rate.push(item.expansion_res_benefit_succ_rate)
    newaccess_res_benefit_succ_rate.push(item.newaccess_res_benefit_succ_rate)
}

function isSetBaseVal (param) {
    // return (param.geo_dim === '省' && !param.city && param.area === '全量小区') || (param.geo_dim === '地市' && param.city && param.city != '全量小区' && param.area === '全量小区') || (param.geo_dim === '区县' && param.city && param.city != '全量小区' && param.area != '全量小区');
    return param === 'wideTable/timeChart'
}
